/*  
    需求：订阅发布方发布的学生消息，并输出到终端。
*/

#include "rclcpp/rclcpp.hpp"
#include "base_interfaces_demo/msg/student.hpp"

using std::placeholders::_1;
using base_interfaces_demo::msg::Student;

//3，定义节点类；
class MinimalSubscription:public rclcpp::Node
{
  public:
    MinimalSubscription(): //类继承的Node构造函数列表初始化
    Node("student_subscription_cpp"),count_(0){
      RCLCPP_INFO(this->get_logger(),"创建订阅节点!");
      Subscription_ = this->create_subscription<Student>("topic_stu",10,std::bind(&MinimalSubscription::stu_callback,this,_1));
    }
    
  private:
    void stu_callback(const Student::SharedPtr msg) const
    {
        RCLCPP_INFO(this->get_logger(),"订阅的学生消息:name=%s,age=%d,height=%.2f",
        msg->name.c_str(),msg->age,msg->height);
    }
    rclcpp::Subscription<Student>::SharedPtr Subscription_;
    size_t count_;
};

int main(int argc, char ** argv)
{
  // 2，初始化ROS2客户端
  rclcpp::init(argc,argv);
  // 4，调用spin函数，并传入节点对象指针；
  rclcpp:spin(std::make_shared<MinimalSubscription>());
  // 5，释放资源
  rclcpp::shutdown();
  return 0;
}